String.prototype.getBytes = function(){
	var cArr = this.match(/[^\x00-\xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length);
}
$(document).ready(function(){
	$('#refresh_code').click(function(){
		var $code = $(this).find('#code_img');
		$code.attr('src',$code.attr('src')+'?rnd='+Math.random());
	});
	/*
	* 验证注册表单
	*/
	var $register_form = $('#register_form');
	jQuery.validator.addMethod("name_length", function(value, element) {
		return $.trim(value).getBytes() >= 4 && $.trim(value).getBytes() <= 14; 
	}, "名号长度不能小于4或大于14");
	$register_form.validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent() );
		},
		rules:{
			name:{required:true,name_length:true},
			password:{
				required:true
			},
			password_repeat:{
				required:true,
				equalTo: "#password"
    		},
    		code:{
    			required:true,
    			remote:{
    				type:'post',
    				url:'/handlers/register_handler.php?action=check_code'
    			}
    		}
		},
		messages:{
			name:{
				required:"名号不能为空"
			},
			email:"请填写正确的邮箱地址格式",
			password:{
				required:'密码不能为空',
				minlength: "密码长度不能小于6个字符",
				maxlength: "密码长度不能大于20个字符"
			},
			password_repeat:{
				required:'密码不能为空',
				equalTo:'两次密码输入不一致'
			},
			code:{
				required:"验证码不能为空",
				remote:"验证码错误"
			}
		}
	});
	/*
	* 提交注册表单
	*/
	$register_form.submit(function(){

		if(!$(this).valid())return false;
		$('#response').html('');
		var $submit = $(this).find('input[type="submit"]');
		var $waiting = $('#waiting'); 

		$submit.fadeOut();
		$waiting.html('<img src="/assets/images/ajax-loader.gif"/ align="absmiddle">&nbsp;处理中，请稍后。');
		$register_form.find("input").attr('disabled','true');

		var name = $.trim($('#name').val());
		var password = $('#password').val();
		var email = $.trim($('#email').val());
		var code = $('#code').val();
		$.post(
			$(this).attr('action'),
			{name:name, password:password, email:email, code:code},
			function(json){
				$waiting.empty();
				if(json.success){
					$('#response').css('color','green')
					.html('注册成功，系统将自动跳转，你也可以点击<a href="dashboard.php">下一步</a>进行跳转。');
					window.location.href='dashboard.php';
				}else{
					$submit.fadeIn();
					$register_form.find("input").removeAttr('disabled');
					$('#code').val('');
					$('#code_img').attr('src',$('#code_img').attr('src')+'?rnd='+Math.random());
					if(json.error_code && json.error_code==1){
						$('#response').css('color','red').html('邮箱已被注册，请换一个邮箱重新注册');
						$('#email').attr('class','error').val('');
					}
				}
			},
			'json'
		);
		return false;
	});
	/*
	* 验证登录表单
	*/
	var $login_form = $('#login_form');
	$login_form.validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent() );
		},
		rules:{
			password:{
				required:true
			},
    		code:{
    			required:true,
    			remote:{
    				type:'post',
    				url:'/handlers/register_handler.php?action=check_code'
    			}
    		}
		},
		messages:{
			email:"请填写正确的邮箱地址格式",
			password:{
				required:'密码不能为空',
				minlength: "密码长度不能小于6个字符",
				maxlength: "密码长度不能大于20个字符"
			},
			code:{
				required:"验证码不能为空",
				remote:"验证码错误"
			}
		}
	});
	/*
	* 提交登录表单
	*/
	$login_form.submit(function(){
		return $(this).valid();
	});
});